package com.project.dao.impl;

import com.project.bean.CutPageBean;
import com.project.bean.LinkBean;
import com.project.dao.ILinkDao;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;

/**
 * 通讯录持久接口实现类
 */
@Repository
public class LinkDaoImpl extends BaseDao implements ILinkDao {

    @Override
    public CutPageBean<LinkBean> cutAll(int pageNO) {
        CutPageBean<LinkBean> cutBean = new CutPageBean<LinkBean>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select  * from t_link limit ?,?");
            //记录数为页码减一，乘以每页显示条数
            ps.setObject(1,(pageNO-1)*CutPageBean.PAGESIZE);
            ps.setObject(2,CutPageBean.PAGESIZE);
            rs= ps.executeQuery();
            while (rs.next()){
                LinkBean linkBean = this.getLink(rs);
                cutBean.getList().add(linkBean);
            }
            ps = con.prepareStatement("select count(*) c from t_link");
            rs =ps.executeQuery();
            if (rs.next()){
                cutBean.setCount(rs.getInt("c"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.close();
        }
        return cutBean;
    }

    @Override
    public void add(LinkBean link) {
        this.setConnection();
        try {

            ps=con.prepareStatement("insert into t_link(l_name,l_birthday,l_phone)"+" values (?,?,?)");
            ps.setObject(1,link.getName());
            ps.setObject(2,link.getBirthday());
            ps.setObject(3,link.getPhone());
            ps.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.close();
        }

    }

    @Override
    public void del(int id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("delete from t_link where pk_linkId =?");
            ps.setObject(1,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.close();
        }

    }

    @Override
    public void update(int id, String phone) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_link set l_phone=? where pk_linkId=? ");
            ps.setObject(1,phone);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.close();
        }
    }

    @Override
    public LinkBean findById(int id) {
        LinkBean link=null;
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_link where pk_linkId=? ");
            ps.setObject(1,id);
            //将查询结果，存放至结果查询中
            rs = ps.executeQuery();
            if(rs.next()){
                 link = this.getLink(rs);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return link;
    }

    private LinkBean getLink(ResultSet rs) throws SQLException {
        LinkBean link = new LinkBean();
        link.setId(rs.getInt("pk_linkId"));
        link.setName(rs.getString("l_name"));
        link.setBirthday(LocalDate.parse(rs.getString("l_birthday")));
        link.setPhone(rs.getString("l_phone"));
        return link;
    }
}
